<?php
class RecordingComponent extends Object {
    var $controller = null;
	var $components = array('Cookie');
	var $settings 	= array();
	var $cookie 	= null;
	var $value 		= null;
	var $urlRedirect = null;
	
	function initialize($controller, $config){
		$this->controller 	= $controller;
		$this->settings 	= array_merge(array('expires' => '7 days'), $config);
		if(empty($this->settings['urlRedirect'])){
			$trig_msg = "Recording::initialize() - Undefined 'urlRedirect' key in [$this->controller->name] Controller::[\$components['Recording']]";
			trigger_error(__($trig_msg, true), E_USER_WARNING);
		}
		
		if(isset($this->controller->params['named']['cookie'])){
			$this->cookie = $this->controller->params['named']['cookie'];
		}else{
			$this->cookie = null;	
		}
		
		if(isset($this->controller->params['named']['value'])){
			$this->value = $this->controller->params['named']['value'];
		}else{
			$this->value = null;	
		}
		
		$this->urlRedirect = $this->settings['urlRedirect'];
		//pr($this->controller->params);
		//pr($this->settings['urlRedirect']);
		//exit();
		//exit();
	}
	
	function startup($controller){
		$controller->autoRender = null;
	}
	
	function __record($cookie, $value, $expires){
		$this->Cookie->write($cookie, $value, true, $expires);
	}
	
	function redirect(){
		$ref = env('HTTP_REFERER');
		if(isset($ref)){
			$this->controller->redirect($ref, null, true);
		}elseif(isset($this->urlRedirect)){
			$this->controller->redirect($this->urlRedirect, null, true);
		}
	}
	
	function __cookieValid($cookie, $value){
		if(isset($this->settings['cookies'][$cookie]) && 
		   in_array($value, $this->settings['cookies'][$cookie])
		){
			return true;
		}else{
			return false;
		}
	}
	
	function save($expires=null){
		if(isset($expires)){
			$this->settings['expires'] = $expires;
		}
		
		if($this->__cookieValid($this->cookie, $this->value)){
			$this->__record($this->cookie, $this->value, $this->settings['expires']);
			return true;
		}
		return false;
	}
}

?>